package com.wlb365.financing.modules.trade.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.constants.SysConstant;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.modules.sys.entity.Dict;
import com.wlb365.financing.modules.sys.service.IDeptService;
import com.wlb365.financing.modules.sys.service.IDictService;
import com.wlb365.financing.modules.trade.request.TradeQueryProcessDataDetailReq;
import com.wlb365.financing.modules.trade.response.QueryPageTradeDTO;
import com.wlb365.financing.modules.trade.response.TradeQueryDetailRes;
import com.wlb365.financing.modules.trade.response.TradeQueryPageRes;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessDataDetailRes;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessDataRes;
import com.wlb365.financing.modules.trade.response.TradeQueryProcessRes;
import com.wlb365.financing.modules.trade.response.data.QuerySchemaByIdRes;
import com.wlb365.financing.modules.trade.service.IDataService;
import com.wlb365.financing.modules.trade.service.ITradeService;
import com.wlb365.financing.modules.trade.vo.TradeQueryVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 交易数据管理类.
 *
 * @author zwh
 * @version 1.0
 */
@RestController
@RequestMapping("trade")
public class TradeController {
    @Autowired
    private ITradeService tradeService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IDictService iDictService;

    @Autowired
    private IDataService dataService;


    /**
     * 交易详情页面.
     * @param id
     * @author lh
     * @return TradeQueryProcessRes
     **/
    @RequiresPermissions("trade:infoView")
    @GetMapping("infoView")
    public ModelAndView infoView(final String id) {
        ModelAndView modelAndView = new ModelAndView("trade/trade_info");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /**
     * 获取阶段详细数据.
     * @param tradeQueryProcessDataDetailReq
     * @author lh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping("processInfoDataDetail")
    public TradeQueryProcessDataDetailRes processInfoDataDetail(TradeQueryProcessDataDetailReq tradeQueryProcessDataDetailReq) {
        TradeQueryProcessDataDetailRes tradeQueryProcessDataDetailRes = tradeService.queryProcessDataDetail(tradeQueryProcessDataDetailReq);
        QuerySchemaByIdRes res = dataService.querySchemaById(tradeQueryProcessDataDetailRes.getTemplateId());
        if(null != res){
            if(null != res.getTemplateData()){
                tradeQueryProcessDataDetailRes.setTemplateData(res.getTemplateData());
            }
        }
        return tradeQueryProcessDataDetailRes;
    }

    /**
     * 获取阶段概要数据.
     * @param processId 流程id
     * @author lh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping("processInfoData")
    public TradeQueryProcessDataRes processInfoData(String processId) {
        TradeQueryProcessDataRes tradeQueryProcessDataRes = tradeService.queryTradeProcessData(processId);
        if(tradeQueryProcessDataRes.getProcessDataList()!=null && tradeQueryProcessDataRes.getProcessDataList().size()>0){
            for(TradeQueryProcessDataRes.TradeProcessDataDTO tradeQueryProcessDataResDto :tradeQueryProcessDataRes.getProcessDataList()){
                Dict dict = iDictService.getByCode(tradeQueryProcessDataResDto.getDataType());
                if(null!=dict){
                    tradeQueryProcessDataResDto.setName(dict.getName());
                }else{
                    tradeQueryProcessDataResDto.setName(tradeQueryProcessDataResDto.getDataType());
                }
            }
        }
        return tradeQueryProcessDataRes;
    }

    /**
     * 获取阶段数据.
     * @param id
     * @author lh
     * @return TradeQueryProcessRes
     **/
    @GetMapping("info")
    public TradeQueryProcessRes info(final String id) {
        TradeQueryProcessRes tradeQueryProcessRes = tradeService.queryTradeProcess(id);
        if(tradeQueryProcessRes.getProcessList()!=null && tradeQueryProcessRes.getProcessList().size()>0){
            for(TradeQueryProcessRes.TradeProcessDTO tradeProcessDataDTO :tradeQueryProcessRes.getProcessList()){
                Dict dict = iDictService.getByCode(tradeProcessDataDTO.getDataType());
                if(null!=dict){
                    tradeProcessDataDTO.setName(dict.getName());
                }
            }
        }
        return tradeQueryProcessRes;
    }

    /**
    * 交易列表页面.
    * @param
    * @author zwh
    * @return org.springframework.web.servlet.ModelAndView
    **/
    @RequiresPermissions("trade:index")
    @GetMapping("index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("trade/trade");
        mv.addObject("supplierList", deptService.listDeptByType(SysConstant.DEPT_TYPE_0));
        mv.addObject("coreList", deptService.listDeptByType(SysConstant.DEPT_TYPE_1));

        return mv;
    }

    /**
     * 交易列表页面.
     * @param
     * @author zwh
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequiresPermissions("trade:generate")
    @GetMapping("generate")
    public ModelAndView generate() {
        ModelAndView mv = new ModelAndView("trade/trade_generate");
        mv.addObject("supplierList", deptService.listDeptByType(SysConstant.DEPT_TYPE_0));
        mv.addObject("coreList", deptService.listDeptByType(SysConstant.DEPT_TYPE_1));

        return mv;
    }

    /**
    * 交易数据列表查询.
    * @param queryVO 查询条件
    * @author zwh
    * @return com.wlb365.financing.common.dto.R
    **/
    @GetMapping(value = "list/data")
    public R listData(final TradeQueryVO queryVO) {
        TradeQueryPageRes res = tradeService.queryTrade(queryVO);

        Page<QueryPageTradeDTO> page = new Page<>();
        page.setCurrent(res.getCurrent());
        page.setSize(res.getSize());
        page.setTotal(res.getTotal());
        page.setRecords(res.getItems());

        return R.ok(page);
    }

    /**
    * 查询交易数据详情.
    * @param id 交易数据id
    * @author zwh
    * @return com.wlb365.financing.common.dto.R
    **/
    @GetMapping(value = "detail/info")
    public R detailInfo(final String id) {
        //String test = "{\"tradeId\":\"1\",\"contractNo\":\"100000001\",\"contractAmount\":1000.00,\"coreEnterpriseId\":\"1\",\"supplierId\":\"1\",\"projectName\":\"地在\",\"tradeDate\":\"20210201\",\"tradeTime\":\"101010\",\"processList\":[{\"contractNo\":\"100000001\",\"stepNum\":1,\"dataType\":\"contractInfo\",\"tradeId\":\"1\",\"status\":\"1\"},{\"contractNo\":\"100000001\",\"stepNum\":2,\"dataType\":\"supplyInfo\",\"tradeId\":\"1\",\"status\":\"1\"},{\"contractNo\":\"100000001\",\"stepNum\":3,\"dataType\":\"acceptanceInfo\",\"tradeId\":\"1\",\"status\":\"1\"},{\"contractNo\":\"100000001\",\"stepNum\":4,\"dataType\":\"settlementInfo\",\"tradeId\":\"1\",\"status\":\"0\"}],\"details\":[{\"stepNum\":1,\"contractNo\":\"100000001\",\"dataType\":\"contractInfo\",\"data\":null,\"status\":\"1\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:13:53\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":2,\"contractNo\":\"100000001\",\"dataType\":\"supplyInfo\",\"data\":null,\"status\":\"1\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:13:54\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":3,\"contractNo\":\"100000001\",\"dataType\":\"acceptanceInfo\",\"data\":null,\"status\":\"0\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:12:36\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"},{\"stepNum\":4,\"contractNo\":\"100000001\",\"dataType\":\"settlementInfo\",\"data\":null,\"status\":\"0\",\"createBy\":\"1\",\"createDate\":\"2021-04-15 16:12:46\",\"updateBy\":\"0\",\"updateDate\":\"2021-04-23 16:11:20\"}]}";
        TradeQueryDetailRes res = tradeService.queryTradeDetail(id);
        if (res != null && ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            return R.ok(res);
        }else{
            return R.fail("模板不存在");
        }
    }

}
